package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcAdvisoryMapper;
import com.csun.cmny.provider.dto.AdvisoryQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcAdvisory;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.OmcAdvisoryService;
import com.csun.cmny.provider.service.RoomService;
import com.csun.cmny.provider.vo.AdvisoryVo;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcAdvisoryServiceImpl extends BaseService<OmcAdvisory> implements OmcAdvisoryService {

    @Resource
    private OmcAdvisoryMapper omcAdvisoryMapper;

    @Resource
    private BedService bedService;

    @Resource
    private RoomService roomService;

    @Override
    public Integer saveAdvisory(OmcAdvisory omcAdvisory, LoginAuthDto loginAuthDto) {

        omcAdvisory.setUpdateInfo(loginAuthDto);

        if(omcAdvisory.isNew()){

            if(omcAdvisory.getBedId() != null && omcAdvisory.getBedId() != 0){

                //如果预约了床位，需要改变床位状态
                Bed bed = bedService.selectById(omcAdvisory.getBedId());
                if(bed == null){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034004);
                }
                //查看床位状态
                if(bed.getStatus() != 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034006);
                }
                //床位状态 0-空床 1-暂出 2-有人 3-咨询入住时的预约 4-入住时的预约 5 - 试入住 6-调房预约
                bed.setStatus(3);
                int ret = bedService.updateById(bed);
                if(ret < 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034005);
                }

                //拼接房间号床位号
                Room room = roomService.selectById(bed.getRoomId());
                String bedNo = room.getRoomNo().toString() + "-" + bed.getBedNo().toString();
                omcAdvisory.setBedNo(bedNo);
            }
            //设定意向房间参数
            if(omcAdvisory.getRoomId() != null){
                Room room = roomService.selectById(omcAdvisory.getRoomId());
                omcAdvisory.setFloorNo(room.getFloorNo());
                omcAdvisory.setBuildingId(room.getBuildingId());
            }else{
                omcAdvisory.setFloorNo(0);
                omcAdvisory.setBuildingId(0);
            }
            return omcAdvisoryMapper.insertSelective(omcAdvisory);
        }else{
            if(omcAdvisory.getBedId() != null && omcAdvisory.getBedId() != 0){

                //取消原来床位的预约状态
                OmcAdvisory advisory = selectByKey(omcAdvisory.getId());
                if(advisory.getBedId() != null){
                    Bed bedCancel = bedService.selectById(advisory.getBedId());
                    bedCancel.setStatus(0);
                    int retCancel = bedService.updateById(bedCancel);
                }

                //如果预约了床位，需要改变床位状态
                Bed bed = bedService.selectById(omcAdvisory.getBedId());
                if(bed == null){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034004);
                }
                //查看床位状态
                if(bed.getStatus() != 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034006);
                }
                //预约床位状态0-没有预约1-已经预约
                bed.setStatus(3);
                int ret = bedService.updateById(bed);
                if(ret < 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034005);
                }

                //拼接房间号床位号
                Room room = roomService.selectById(bed.getRoomId());
                String bedNo = room.getRoomNo().toString() + "-" + bed.getBedNo().toString();
                omcAdvisory.setBedNo(bedNo);
            }

            //设定意向房间参数
            if(omcAdvisory.getRoomId() != null && !omcAdvisory.getRoomId().equals(0)){
                Room room = roomService.selectById(omcAdvisory.getRoomId());
                omcAdvisory.setFloorNo(room.getFloorNo());
                omcAdvisory.setBuildingId(room.getBuildingId());
            }
            return omcAdvisoryMapper.updateByPrimaryKeySelective(omcAdvisory);
        }
    }

    @Override
    public Integer deleteAdvisoryById(Long id, LoginAuthDto loginAuthDto) {

            // 校验咨询记录是否存在
            OmcAdvisory omcAdvisoryQuery = omcAdvisoryMapper.selectByPrimaryKey(id);
            if (PublicUtil.isEmpty(omcAdvisoryQuery)) {
                throw new OmcBizException(ErrorCodeEnum.OMC10034001);
            }
            if(omcAdvisoryQuery.getBedId() != null){
                throw new OmcBizException(ErrorCodeEnum.OMC10034007);
            }
            return omcAdvisoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo queryAdvisoryListWithPage(AdvisoryQueryDto advisoryQueryDto) {

        PageHelper.startPage(advisoryQueryDto.getPageNum(), advisoryQueryDto.getPageSize());
        List<AdvisoryVo> advisoryVoList = omcAdvisoryMapper.queryAdvisoryListWithPage(advisoryQueryDto);
        return new PageInfo<>(advisoryVoList);
    }

    @Override
    public Integer updateAdvisory(OmcAdvisory omcAdvisory) {
        return omcAdvisoryMapper.updateByPrimaryKey(omcAdvisory);
    }


}
